﻿Partial Public Class DatabaseListControl

    Private Sub DatabaseListControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        AddHandler g_album_mgr.AlbumLoaded, AddressOf OnAlbumLoaded
    End Sub

    Protected Overrides Function MeasureOverride(ByVal constraint As System.Windows.Size) As System.Windows.Size
        If Double.IsInfinity(constraint.Width) Then Return New Size(100, 100)
        Return constraint
    End Function

    Private Delegate Sub DlgOnAlbumLoaded()
    Private Sub OnAlbumLoaded()
        If Not Dispatcher.CheckAccess() Then
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New DlgOnAlbumLoaded(AddressOf OnAlbumLoaded))
        Else
            lvw.Items.Clear()
            If g_album_mgr Is Nothing OrElse g_album_mgr.Photos Is Nothing Then Return
            For Each row As DataRow In g_album_mgr.Photos.Rows

                Try
                    Dim pnl As New WrapPanel
                    pnl.HorizontalAlignment = Windows.HorizontalAlignment.Left

                    Dim img As New Image
                    img.Width = THUMBNAIL_SIZE
                    img.Height = THUMBNAIL_SIZE
                    img.Stretch = Stretch.Uniform
                    img.StretchDirection = StretchDirection.Both
                    img.HorizontalAlignment = Windows.HorizontalAlignment.Center
                    img.VerticalAlignment = Windows.VerticalAlignment.Center
                    Dim b() As Byte = CType(row.Item("Thumbnail"), Byte())
                    Dim bi As BitmapImage = ConvertBytesToImage(b)
                    img.Source = bi

                    img.VerticalAlignment = Windows.VerticalAlignment.Top
                    pnl.Children.Add(img)

                    Dim title As New Label
                    title.HorizontalContentAlignment = Windows.HorizontalAlignment.Left
                    Try
                        title.Content = CType(row.Item("ImageName"), String)
                        title.Foreground = Brushes.Gray
                    Catch
                        title.Content = "NULL"
                        title.Foreground = Brushes.LightGray
                        title.FontWeight = FontWeights.Bold
                        title.FontStyle = FontStyles.Oblique
                    End Try


                    Dim pnl2 As New StackPanel
                    pnl.Children.Add(pnl2)
                    pnl2.Children.Add(title)

                    Dim flowdoc As New FlowDocumentScrollViewer
                    flowdoc.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto
                    flowdoc.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
                    Dim w As Integer = 0
                    If Not Double.IsInfinity(lvw.Width) And Not Double.IsNaN(lvw.Width) Then
                        w = lvw.ActualWidth - THUMBNAIL_SIZE
                    End If
                    If w > 0 Then flowdoc.Width = w
                    flowdoc.Margin = New Thickness(0)
                    pnl2.Children.Add(flowdoc)

                    Dim doc As New FlowDocument
                    flowdoc.Document = doc

                    Dim details As New Paragraph
                    doc.Blocks.Add(details)
                    details.SetValue(Control.FontSizeProperty, 12.0)

                    Dim brush As Integer = 0
                    Dim mybrushes() As Brush = {Brushes.MediumVioletRed, Brushes.Green, Brushes.Blue}
                    Dim nullcount As Integer = 0
                    Dim nullsparsecount As Integer = 0
                    For i As Integer = 0 To g_album_mgr.Photos.Columns.Count - 1
                        Dim val As Object = row.Item(i)
                        Dim isnull As Boolean = False
                        If TypeOf val Is System.DBNull Then
                            isnull = True
                        ElseIf TypeOf val Is Microsoft.SqlServer.Types.SqlGeography Then
                            Dim g As Microsoft.SqlServer.Types.SqlGeography = CType(val, Microsoft.SqlServer.Types.SqlGeography)
                            isnull = g.IsNull
                        ElseIf TypeOf val Is Microsoft.SqlServer.Types.SqlGeometry Then
                            Dim g As Microsoft.SqlServer.Types.SqlGeometry = CType(val, Microsoft.SqlServer.Types.SqlGeometry)
                            isnull = g.IsNull
                        End If

                        Dim colname As String = g_album_mgr.Photos.Columns.Item(i).ColumnName
                        If isnull Then
                            nullcount += 1
                            If ExifMetadata.MetaMapper.IsSparse(colname) Then nullsparsecount += 1
                        Else
                            Dim lbl As New Run
                            Dim colval As String = val.ToString()
                            If colval.Trim <> "" Then
                                lbl.Text = colname & "=" & val.ToString() & " "
                                brush += 1
                                Math.DivRem(brush, mybrushes.Count, brush)
                                lbl.Foreground = mybrushes(brush)
                                details.Inlines.Add(lbl)
                            End If
                        End If
                    Next

                    Dim stats As New Run
                    stats.Text = "NULLS:" & nullcount & " SPARSE NULLS:" & nullsparsecount
                    details.Inlines.Add(New LineBreak)
                    details.Inlines.Add(stats)


                    Dim lvi As New ListViewItem
                    lvi.Content = pnl
                    lvi.Tag = row
                    lvw.Items.Add(lvi)
                Catch ex As Exception
                    ReportException(ex)
                End Try
            Next

        End If
    End Sub

    Private Sub lvw_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lvw.MouseDoubleClick
        If lvw.SelectedItem Is Nothing Then Return
        Dim lvi As ListViewItem = lvw.SelectedItem
        Dim row As DataRow = CType(lvi.Tag, DataRow)
        If row Is Nothing Then Return
        ImageViewerForm.ShowFullImage(row)
    End Sub

    Private Sub lvw_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lvw.SelectionChanged
        Dim lvi As ListViewItem = lvw.SelectedItem
        If lvi Is Nothing Then Return
        Dim row As DataRow = CType(lvi.Tag, DataRow)
        g_currentphoto.Row = row
    End Sub
End Class
